/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.TypedElement;

/**
 * Command to set the type of a TypedElement.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Element
 * <li>Type - if null, it means any existing type will be unset.
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>None
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class SetTypeCommand extends Command {

	private TypedElement element;
	private Type oldType, type;

	public SetTypeCommand(final TypedElement element) {
		this(element, null);
	}

	public SetTypeCommand(final TypedElement element, final Type type) {
		setElement(element);
		setType(type);
	}

	public void setElement(final TypedElement part) {
		this.element = part;
	}

	public void setType(final Type type) {
		this.type = type;
	}

	/*
	 * @Override public boolean canExecute() { return element != null; }
	 */

	@Override
	public void execute() {
		oldType = element.getType();
		element.setType(type);
	}

	@Override
	public void undo() {
		element.setType(oldType);
		oldType = null;
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void dispose() {
		element = null;
		type = null;
		oldType = null;
	}
}
